//NEWEST! 12/7/2012!
import java.util.ArrayList;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Antonio
 */
public class CTCGUI extends javax.swing.JFrame 
{

    public static String status = "";
    public static JLabel errmsg; 
    static JFrame frame;
    static CTCGUI gui;
   static TrackControllerModule tcm;
    static TrackModel trackM;
    
    CTCBundle bun = new CTCBundle();
    
    
    String appending = "";
    
    ArrayList<String> bundleArr = new ArrayList<String>();
    
    Date displayDate = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   
    public static int trainID;
    public String line;
    boolean errorCheck = false;
    
    public CTCGUI() 
    {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Tabs = new javax.swing.JTabbedPane();
        SystemDisplayPanel = new javax.swing.JPanel();
        TransitSystemScroll = new javax.swing.JScrollPane();
        TransitSystemTextArea = new javax.swing.JTextArea();
        UpdateButton = new javax.swing.JButton();
        AuthorityAndSpeedPanel = new javax.swing.JPanel();
        AuthorityPanel = new javax.swing.JPanel();
        AuthorityLabel = new javax.swing.JLabel();
        AuthorityTextField = new javax.swing.JTextField();
        AuthorityButton = new javax.swing.JButton();
        SugAndMaxSpeedPanel = new javax.swing.JPanel();
        SugSpeedLabel = new javax.swing.JLabel();
        MaxSpeedLabel = new javax.swing.JLabel();
        SugSpeedTextField = new javax.swing.JTextField();
        MaxSpeedTextField = new javax.swing.JTextField();
        SugAndMaxSpeedButton = new javax.swing.JButton();
        ScheduleTrainPanel = new javax.swing.JPanel();
        DepartTimePanel = new javax.swing.JPanel();
        DepartTimeLabel = new javax.swing.JLabel();
        DepartTimeTextFieldHR = new javax.swing.JTextField();
        DepartTimeButton = new javax.swing.JButton();
        DepartTimeLabel2 = new javax.swing.JLabel();
        DepartTimeTextFieldMIN = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        RouteComboBox = new javax.swing.JComboBox();
        RouteLabel = new javax.swing.JLabel();
        RouteLineButton = new javax.swing.JButton();
        ModifyTrackPanel = new javax.swing.JPanel();
        LinePanel = new javax.swing.JPanel();
        PickLineLabel = new javax.swing.JLabel();
        PickLineComboBox = new javax.swing.JComboBox();
        BlockPanel = new javax.swing.JPanel();
        BlockLabel = new javax.swing.JLabel();
        BlockTextField = new javax.swing.JTextField();
        CloseTrackButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CTC");

        SystemDisplayPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Transit System", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        TransitSystemTextArea.setColumns(20);
        TransitSystemTextArea.setRows(5);
        TransitSystemScroll.setViewportView(TransitSystemTextArea);

        UpdateButton.setText("Update");
        UpdateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpdateButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout SystemDisplayPanelLayout = new javax.swing.GroupLayout(SystemDisplayPanel);
        SystemDisplayPanel.setLayout(SystemDisplayPanelLayout);
        SystemDisplayPanelLayout.setHorizontalGroup(
            SystemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TransitSystemScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
            .addGroup(SystemDisplayPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(UpdateButton)
                .addContainerGap())
        );
        SystemDisplayPanelLayout.setVerticalGroup(
            SystemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, SystemDisplayPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(UpdateButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TransitSystemScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE))
        );

        Tabs.addTab("System Display", SystemDisplayPanel);

        AuthorityPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Set Authority", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        AuthorityLabel.setText("Authority:");

        AuthorityButton.setText("Send");
        AuthorityButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AuthorityButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AuthorityPanelLayout = new javax.swing.GroupLayout(AuthorityPanel);
        AuthorityPanel.setLayout(AuthorityPanelLayout);
        AuthorityPanelLayout.setHorizontalGroup(
            AuthorityPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AuthorityPanelLayout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(AuthorityPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(AuthorityButton)
                    .addGroup(AuthorityPanelLayout.createSequentialGroup()
                        .addComponent(AuthorityLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AuthorityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        AuthorityPanelLayout.setVerticalGroup(
            AuthorityPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AuthorityPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(AuthorityPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AuthorityLabel)
                    .addComponent(AuthorityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AuthorityButton)
                .addContainerGap(96, Short.MAX_VALUE))
        );

        SugAndMaxSpeedPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Set Suggested and Max Speed", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        SugSpeedLabel.setText("Suggested Speed:");

        MaxSpeedLabel.setText("Max Speed:");

        SugAndMaxSpeedButton.setText("Send");
        SugAndMaxSpeedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SugAndMaxSpeedButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout SugAndMaxSpeedPanelLayout = new javax.swing.GroupLayout(SugAndMaxSpeedPanel);
        SugAndMaxSpeedPanel.setLayout(SugAndMaxSpeedPanelLayout);
        SugAndMaxSpeedPanelLayout.setHorizontalGroup(
            SugAndMaxSpeedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SugAndMaxSpeedPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SugAndMaxSpeedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(SugAndMaxSpeedButton)
                    .addGroup(SugAndMaxSpeedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(SugAndMaxSpeedPanelLayout.createSequentialGroup()
                            .addComponent(SugSpeedLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(SugSpeedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(SugAndMaxSpeedPanelLayout.createSequentialGroup()
                            .addGap(31, 31, 31)
                            .addComponent(MaxSpeedLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(MaxSpeedTextField))))
                .addContainerGap(387, Short.MAX_VALUE))
        );
        SugAndMaxSpeedPanelLayout.setVerticalGroup(
            SugAndMaxSpeedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SugAndMaxSpeedPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SugAndMaxSpeedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SugSpeedLabel)
                    .addComponent(SugSpeedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(SugAndMaxSpeedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(MaxSpeedLabel)
                    .addComponent(MaxSpeedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SugAndMaxSpeedButton)
                .addContainerGap(76, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout AuthorityAndSpeedPanelLayout = new javax.swing.GroupLayout(AuthorityAndSpeedPanel);
        AuthorityAndSpeedPanel.setLayout(AuthorityAndSpeedPanelLayout);
        AuthorityAndSpeedPanelLayout.setHorizontalGroup(
            AuthorityAndSpeedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(AuthorityPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(SugAndMaxSpeedPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        AuthorityAndSpeedPanelLayout.setVerticalGroup(
            AuthorityAndSpeedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AuthorityAndSpeedPanelLayout.createSequentialGroup()
                .addComponent(AuthorityPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SugAndMaxSpeedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 48, Short.MAX_VALUE))
        );

        Tabs.addTab("Authority and Speed", AuthorityAndSpeedPanel);

        DepartTimePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Set Departure Time", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        DepartTimeLabel.setText("Departure Time (Military Time):");

        DepartTimeButton.setText("Send");
        DepartTimeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DepartTimeButtonActionPerformed(evt);
            }
        });

        DepartTimeLabel2.setText(":");

        javax.swing.GroupLayout DepartTimePanelLayout = new javax.swing.GroupLayout(DepartTimePanel);
        DepartTimePanel.setLayout(DepartTimePanelLayout);
        DepartTimePanelLayout.setHorizontalGroup(
            DepartTimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, DepartTimePanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(DepartTimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(DepartTimePanelLayout.createSequentialGroup()
                        .addComponent(DepartTimeButton)
                        .addGap(440, 440, 440))
                    .addGroup(DepartTimePanelLayout.createSequentialGroup()
                        .addComponent(DepartTimeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DepartTimeTextFieldHR, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DepartTimeLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DepartTimeTextFieldMIN, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(430, 430, 430))))
        );
        DepartTimePanelLayout.setVerticalGroup(
            DepartTimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DepartTimePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(DepartTimePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DepartTimeLabel)
                    .addComponent(DepartTimeTextFieldHR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DepartTimeLabel2)
                    .addComponent(DepartTimeTextFieldMIN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(DepartTimeButton)
                .addContainerGap(63, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Route Train", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        RouteComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Red Line", "Green Line" }));

        RouteLabel.setText("Pick Line:");

        RouteLineButton.setText("Send");
        RouteLineButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RouteLineButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(RouteLineButton)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(RouteLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(RouteComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RouteComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RouteLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(RouteLineButton)
                .addContainerGap(64, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout ScheduleTrainPanelLayout = new javax.swing.GroupLayout(ScheduleTrainPanel);
        ScheduleTrainPanel.setLayout(ScheduleTrainPanelLayout);
        ScheduleTrainPanelLayout.setHorizontalGroup(
            ScheduleTrainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(DepartTimePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        ScheduleTrainPanelLayout.setVerticalGroup(
            ScheduleTrainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ScheduleTrainPanelLayout.createSequentialGroup()
                .addComponent(DepartTimePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 119, Short.MAX_VALUE))
        );

        Tabs.addTab("Schedule Train", ScheduleTrainPanel);

        ModifyTrackPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        LinePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Line", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        PickLineLabel.setText("Pick Line:");

        PickLineComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Red Line", "Green Line" }));

        javax.swing.GroupLayout LinePanelLayout = new javax.swing.GroupLayout(LinePanel);
        LinePanel.setLayout(LinePanelLayout);
        LinePanelLayout.setHorizontalGroup(
            LinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(LinePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(PickLineLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PickLineComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        LinePanelLayout.setVerticalGroup(
            LinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(LinePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(LinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PickLineLabel)
                    .addComponent(PickLineComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(128, Short.MAX_VALUE))
        );

        BlockPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Block", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        BlockLabel.setText("Block:");

        CloseTrackButton.setText("Close Track");
        CloseTrackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseTrackButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout BlockPanelLayout = new javax.swing.GroupLayout(BlockPanel);
        BlockPanel.setLayout(BlockPanelLayout);
        BlockPanelLayout.setHorizontalGroup(
            BlockPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(BlockPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(BlockPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(CloseTrackButton)
                    .addGroup(BlockPanelLayout.createSequentialGroup()
                        .addComponent(BlockLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BlockTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(443, Short.MAX_VALUE))
        );
        BlockPanelLayout.setVerticalGroup(
            BlockPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(BlockPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(BlockPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BlockLabel)
                    .addComponent(BlockTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CloseTrackButton)
                .addContainerGap(91, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout ModifyTrackPanelLayout = new javax.swing.GroupLayout(ModifyTrackPanel);
        ModifyTrackPanel.setLayout(ModifyTrackPanelLayout);
        ModifyTrackPanelLayout.setHorizontalGroup(
            ModifyTrackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(LinePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(BlockPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        ModifyTrackPanelLayout.setVerticalGroup(
            ModifyTrackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ModifyTrackPanelLayout.createSequentialGroup()
                .addComponent(LinePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BlockPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 46, Short.MAX_VALUE))
        );

        Tabs.addTab("Modify Track", ModifyTrackPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Tabs)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Tabs)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SugAndMaxSpeedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SugAndMaxSpeedButtonActionPerformed
        
    	new Thread()
    	{
            public void run()
            {
                String sugSpeed, maxSpeed, trainIDString;
                
                trainIDString = trainID + "";;
                
                sugSpeed = SugSpeedTextField.getText();
                
                maxSpeed = MaxSpeedTextField.getText();
                bundle(null, sugSpeed, maxSpeed, null, null, null, trainIDString);
            }
            
         }.start();

        
    }//GEN-LAST:event_SugAndMaxSpeedButtonActionPerformed

    private void AuthorityButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AuthorityButtonActionPerformed
        
        new Thread()
	{
            public void run()
	    {
                String authority, trainIDString;
                
              
                trainIDString = trainID + "";
        
                authority = AuthorityTextField.getText();

                bundle(authority, null, null, null, null, null, trainIDString);
               
            }
            
         }.start();
    }//GEN-LAST:event_AuthorityButtonActionPerformed

    private void DepartTimeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DepartTimeButtonActionPerformed
        
        new Thread()
	{
            public void run()
	    {
                String departTimeHR, departTimeMIN, departTime, trainIDString;
                
             
                trainIDString = trainID + "";
        
                departTimeHR = DepartTimeTextFieldHR.getText();
                departTimeMIN = DepartTimeTextFieldMIN.getText();
                
                departTime = departTimeHR + departTimeMIN;

                bundle(null, null, null, departTime, null, null, trainIDString);
               
            }
            
         }.start();
    }//GEN-LAST:event_DepartTimeButtonActionPerformed

    private void CloseTrackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseTrackButtonActionPerformed
        
        new Thread()
	{
            public void run()
	    {
                String line, block;
              
        
               // section = SectionTextField.getText();
                
                
               line = (String)PickLineComboBox.getSelectedItem();
  
                block = BlockTextField.getText();

                bundle(null, null, null, null, line, block, null);
               
            }
            
         }.start();
    }//GEN-LAST:event_CloseTrackButtonActionPerformed

    private void RouteLineButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {//GEN-FIRST:event_RouteLineButtonActionPerformed
        new Thread()
        {
            public void run()
            {
               String block, trainIDString;
                            
               trainIDString = trainID + "";
               
      
                
               line = (String)RouteComboBox.getSelectedItem();
               
               if(line.equals("Red Line"))
               {
            	   
               }
  
               if(line.equals("Green Line"))
               {
            	   
               }
  


                bundle(null, null, null, null, line, null, trainIDString);
               
            }
            
         }.start();
    }//GEN-LAST:event_RouteLineButtonActionPerformed

    private void UpdateButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {
//    	 new Thread()
//         {
//             public void run()
//             {
//                String block, trainIDString;
//                             
//                
//
//
//                 bundle(null, null, null, null, line, null, trainIDString);
//                
//             }
//             
//          }.start();
    }


    
    public void bundle(String authP, String sugSpeedP, String maxSpeedP, String departTimeP, String lineP, String blockP, String trainIDP)
    { 
        
        String departTime, departTimeHR, departTimeMIN;
        char departTimeChar[];

        System.out.println(authP);
        System.out.println(trainIDP);
        if(authP != null && trainIDP != null)
        {
            try
            {
                bun.authority = Integer.parseInt(authP);
            }
            catch(NumberFormatException  e)
            {
                errorCheck = true;
                errormsg();
            }
        }
        
        if(sugSpeedP != null && trainIDP != null)
        {
            try
            {
                bun.speed = Integer.parseInt(sugSpeedP);
            }
            catch(NumberFormatException  e)
            {
                errorCheck = true;
                errormsg();
            }
        }
        
        if(maxSpeedP != null && trainIDP != null)
        {
            try
            {
                bun.maxSpeed = Integer.parseInt(maxSpeedP);
            }
            catch(NumberFormatException  e)
            {
                errorCheck = true;
                errormsg();
            }
        }
             
        if(departTimeP != null && trainIDP != null)
        {
            try
            {
                bun.departTime = Integer.parseInt(departTimeP);
            }
            catch(NumberFormatException  e)
            {
                errorCheck = true;
                errormsg();
            }
        }

                
        if(blockP != null)
        {
            try
            {
                bun.block = Integer.parseInt(blockP);
            }
            catch(NumberFormatException  e)
            {
                errorCheck = true;
                errormsg();
            }
        }
                            
        try 
        {
            Thread.sleep(3000);
            
        } 
        
        catch (InterruptedException e) 
        {
                e.printStackTrace();
        }
        
        if(errorCheck == false)
        {
            System.out.println("CTC: CTC Bundle sent.");

            if(bundleArr.isEmpty())
            {
                bundleArr.add("");
                bundleArr.add("");
                bundleArr.add("");
                bundleArr.add("");
                bundleArr.add("");
                bundleArr.add("");
                bundleArr.add("");
                bundleArr.add("");
            }

            if(authP != null)
            {
                if(!(authP.equals(bundleArr.get(0))))
                {
                    bundleArr.set(0,authP);
                }
            }

            if(sugSpeedP != null)
            {
                if(!sugSpeedP.equals(bundleArr.get(1)))
                {
                    bundleArr.set(1, sugSpeedP);
                }
            }

            if(maxSpeedP != null)
            {
                if(!maxSpeedP.equals(bundleArr.get(2)))
                {
                    bundleArr.set(2, maxSpeedP);
                }
            }
            
            if(departTimeP != null)
            {
                if(!(departTimeP.equals(bundleArr.get(3))))
                {
                    bundleArr.set(3,departTimeP);
                }
            }

            if(lineP != null)
            {
                if(!lineP.equals(bundleArr.get(4)))
                {
                    bundleArr.set(4, lineP);
                }
            }

            if(blockP != null)
            {
                if(!blockP.equals(bundleArr.get(5)))
                {
                    bundleArr.set(5, blockP);
                }
            }
            
            if(trainIDP != null)
            {
            	if(!trainIDP.equals(bundleArr.get(6)))
                {
                    bundleArr.set(6, trainIDP);
                }
            }
            

            System.out.println("CTC: Bundle sent to TrackControlModule");

            displayDate = new Date();

            if(authP != null)
            {
            	bun.authority = Integer.parseInt(authP);
                appending = appending + "\n" + format.format(displayDate) + ": Authority of " + authP + " blocks has been sent to Train " + trainIDP;
            }
            
            else
            {
            	bun.authority = 0;
            }
            
            if(sugSpeedP != null)
            {
            	bun.speed = Integer.parseInt(sugSpeedP);
                appending = appending + "\n" + format.format(displayDate) + ": Suggested speed of " + sugSpeedP + "m/s has been sent to Train " +trainIDP;
            }
            
            else
            {
            	bun.speed = 0;
            }
            
            if(maxSpeedP !=null)
            {
            	bun.maxSpeed = Integer.parseInt(maxSpeedP);
                appending = appending + "\n" + format.format(displayDate) + ": Max speed of " + maxSpeedP + "m/s has been sent to Train " + trainIDP;
            }      
            
            else
            {
            	bun.maxSpeed = 0;
            }

            if(departTimeP != null)
            {
                departTimeChar = departTimeP.toCharArray();
                
                departTimeHR = "" + departTimeChar[0] + "" +departTimeChar[1];
                
                departTimeMIN = "" + departTimeChar[2] + "" +departTimeChar[3];
                
                bun.departTime = Integer.parseInt(departTimeMIN);
                
                appending = appending + "\n" + format.format(displayDate) + ": Departure time of " + departTimeHR + ":" + departTimeMIN + " has been sent to Train " +trainIDP;
            }
            
            else
            {
            	bun.departTime = 0;
            }

            if(lineP != null && blockP != null)
            {
            	if(lineP.equals("Red Line"))
            	{
                	bun.line = 1;
            	}
            	
            	if(lineP.equals("Green Line"))
            	{
                	bun.line = 2;
            	}
            	
            	bun.block = Integer.parseInt(blockP);
            	
               appending = appending + "\n" + format.format(displayDate) + ": Block no. " + blockP + " has been closed on line " + lineP + " for maintenance";

            }
            
            else 
            {
            	bun.line = 0;
            	bun.block = 0;
            }

            TransitSystemTextArea.setText(appending);
            TransitSystemTextArea.setCaretPosition(TransitSystemTextArea.getDocument().getLength());
            tcm.receiveBundle(bun);
        }
    }
    
    public void errormsg()
    {		
        status = "Please enter an integer!";
        frame = new JFrame("ERROR MESSAGE");
        frame.setBounds(200, 200, 400, 100);
        frame.getContentPane().setLayout(new GridLayout(3,1,0,0));
        errmsg = new JLabel(status);
        frame.getContentPane().add(errmsg);
        frame.setVisible(true);
        
    }
    
    public static void main(String args[]) 
    {

      	
    	
//    	ArrayList <Block> redLine = new ArrayList<Block>();
//    	ArrayList <Block> greenLine = new ArrayList<Block>();
//    	
//    	redLine.add(s);
//    	
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try 
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } 
        catch (ClassNotFoundException ex) 
        {
            java.util.logging.Logger.getLogger(CTCGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        
        catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(CTCGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        
        catch (IllegalAccessException ex) 
        {
            java.util.logging.Logger.getLogger(CTCGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        
        catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CTCGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

         
        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                try 
                {

                        gui = new CTCGUI();
                        gui.setVisible(true);
                } 
                catch (Exception e) 
                {
                        e.printStackTrace();
                }
            }
        });
        
        EventQueue.invokeLater(new Runnable() 
        {
            public void run()
            {
                try 
                {

                    tcm = new TrackControllerModule(gui);
                } 
                
                catch (Exception e) 
                {
                        e.printStackTrace();
                }
            }
        });
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel AuthorityAndSpeedPanel;
    private javax.swing.JButton AuthorityButton;
    private javax.swing.JLabel AuthorityLabel;
    private javax.swing.JPanel AuthorityPanel;
    private javax.swing.JTextField AuthorityTextField;
    private javax.swing.JLabel BlockLabel;
    private javax.swing.JPanel BlockPanel;
    private javax.swing.JTextField BlockTextField;
    private javax.swing.JButton CloseTrackButton;
    private javax.swing.JButton DepartTimeButton;
    private javax.swing.JLabel DepartTimeLabel;
    private javax.swing.JLabel DepartTimeLabel2;
    private javax.swing.JPanel DepartTimePanel;
    private javax.swing.JTextField DepartTimeTextFieldHR;
    private javax.swing.JTextField DepartTimeTextFieldMIN;
    private javax.swing.JPanel LinePanel;
    private javax.swing.JLabel MaxSpeedLabel;
    private javax.swing.JTextField MaxSpeedTextField;
    private javax.swing.JPanel ModifyTrackPanel;
    private javax.swing.JComboBox PickLineComboBox;
    private javax.swing.JLabel PickLineLabel;
    private javax.swing.JComboBox RouteComboBox;
    private javax.swing.JLabel RouteLabel;
    private javax.swing.JButton RouteLineButton;
    private javax.swing.JPanel ScheduleTrainPanel;
    private javax.swing.JButton SugAndMaxSpeedButton;
    private javax.swing.JPanel SugAndMaxSpeedPanel;
    private javax.swing.JLabel SugSpeedLabel;
    private javax.swing.JTextField SugSpeedTextField;
    private javax.swing.JPanel SystemDisplayPanel;
    private javax.swing.JTabbedPane Tabs;
    private javax.swing.JScrollPane TransitSystemScroll;
    public javax.swing.JTextArea TransitSystemTextArea;
    private javax.swing.JButton UpdateButton;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
